// pages/personal/personal_shops/personal_shops.js
var utils = require('../../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;
var codeName = getApp().globalData.codeName;
var userId = getApp().globalData.userId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isImg:false,
    filePaths: [],  //上传图片的列表
    imglist:"",
    ispos:"leftPos",  //左隐藏
    locationAddress:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    utils.http(httpUrl +"getMyMerchant",{
      code:codeName,
      userid:userId
    },function(res){
      console.log(res);
      if (res.merchant[0].image!=""){
        that.setData({
          isImg:true
        })
      }
      var levelName=""
      if (res.merchant[0].level==1){
        levelName="普通商家"
      }else{
        levelName="金牌商家"
      }
      that.setData({
        merchant: res.merchant[0],
        imglist: res.merchant[0].image,
        level:levelName,
        modelid:res.merchant[0].modelid,
        categoryId: res.merchant[0].categoryId,
        locationAddress: res.merchant[0].location

      })
    })

    that.getshopType();
  },
//获取商家分类列表
  getshopType:function(e){
    var that=this;
    utils.http(httpUrl + "getmodelList", {
      code: codeName,
      cityid: wx.getStorageSync("city").cityId
    }, function (res) {
      console.log(res);
      that.setData({
        list: res.merchantlist
      })
    })
  },

  //上传头像
  oNuploadTap: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        that.setData({
          tempFilePaths: tempFilePaths[0],
          isImg:true
        })
        wx.uploadFile({
          url: httpUrl + 'uploadImg',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success: function (e) {
            console.log(e);
            that.setData({
              imglist: JSON.parse(e.data).imgs
            })
          }

        })
      }
    })
  },

  //产品详情图片上传
  oNproductShlTap: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        // that.setData({
        //   tempFilePaths: tempFilePaths[0], 
        // })
        wx.uploadFile({
          url: httpUrl + 'uploadImg',
          filePath: tempFilePaths[0],
          name: 'file',
          success: function (e) {
            that.setData({
              isImg: true,
              imageList: JSON.parse(e.data).imgs
            })
            var listku = that.data.listku;
            if (listku == undefined) {
              listku = []
            }
            listku.push(that.data.imageList);
            that.setData({
              listku: listku
            })
            console.log(listku);
          }

        })
      }
    })
  },
  //点击商家分类
  clickTypeTap:function(e){
      this.setData({
        ispos: "rightPos"  //有显示 
      })
  },
  
  oNlistTap: function (e) {
    var name = e.currentTarget.dataset.name;
    var id = e.currentTarget.dataset.id;
    this.setData({
      modelid:name,
      categoryId: id,
      ispos: "leftPos"  //隐藏
    })
    
  },
  //掉出地图页面
  oNlocationTap: function (e) {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        console.log(res.address)
        that.setData({
          locationAddress:res.address
        })
     
      }
    })
  },
  //提交接口
  formSubmit:function(e){
    var that=this;
    var name = e.detail.value.name;
    var linkman = e.detail.value.linkman
    var telephone = e.detail.value.telephone
    var introduce = e.detail.value.introduce
    var productShl = e.detail.value.productShl
    var imagelist = that.data.listku.join(",");
    utils.http(httpUrl +"updateMyMerchant",{
      id: that.data.merchant.id,
      code: codeName,
      name: name,//商家名字
      images: that.data.imglist,//头像
      linkman: linkman, //联系人名字
      telephone: telephone, //电话
      introduce: introduce,//商家信息
      location: that.data.locationAddress, //地址
      modelid: that.data.modelid, //分类名称
      userid: userId, //用户提高了
      cityid: wx.getStorageSync("city").cityid, //城市ID
      level: that.data.merchant.level,//等级
      categoryId: that.data.categoryId,//分类ID
      price: that.data.totalprice, //价格
      productShl: productShl, //产品介绍
      imageList: imagelist   //商家详情图片   
    },function(res){
      console.log(res);
      if (res.status == 200) {
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: '失败',
          icon: 'loading',
          duration: 2000
        })
      }
    })
  }
  // //进入入驻商圈的页面
  // oNbusinesssettledTap:function(e){
  //   wx.navigateTo({
  //     url: '../../business_settled/business_settled',
  //   })
  // }
 
})